package cn.wildfire.chat.app.main;

import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.wildfire.chat.app.extendsion.classes.ResponseEntity;
import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.app.extendsion.ui.ToastTip;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.utils.InputMethodUtils;
import cn.wildfirechat.remote.ChatManager;

/**
 * @Author Administrator
 * @Date 2019/9/21
 */
public class ReportSuggestionActivity extends WfcBaseActivity {

    @BindView(R2.id.et_appeal_content)
    EditText etAppealContent;
    @BindView(R2.id.tv_total_count)
    TextView tvTotalCount;

    Handler handler = new Handler();

    @Override
    protected void afterViews() {
        super.afterViews();
        etAppealContent.setHint(getString(R.string.et_report_suggestion_hint));

        etAppealContent.setFocusable(true);
        etAppealContent.setFocusableInTouchMode(true);
        etAppealContent.requestFocus();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodUtils.showOrHide(ReportSuggestionActivity.this);
            }
        },500);


        etAppealContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvTotalCount.setText("" + s.length() + "/200");
                if (s.length() == 200) {
                    ToastTip.tip("投诉建议不能超过200个字符!");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_manual_appeal;
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 2);
        setReplaceTitle(null, R.string.title_report_suggestion);
        setSearchLayoutVisibility(false);
    }

    @OnClick(R2.id.btn_set_security_question_finish)
    public void onClick(View view) {
        String trim = etAppealContent.getText().toString().trim();
        if (TextUtils.isTextEmpty(trim)) {
            ToastTip.tip(getString(R.string.content_can_not_empty));
            return;
        }
        if (trim.length() > 200) {
            ToastTip.tip(getString(R.string.content_length_is_long));
            return;
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("content", trim);
        params.put("uid", ChatManager.Instance().getUserId());

        OkHttpRequest.getInstance(this).requestAsynJsonObject(ServerUrlClient.REQUEST_COMPLAINT, params, new ReqCallBack<Object>() {
            @Override
            public void onReqSuccess(Object result) {
                if (!TextUtils.isTextEmpty(result.toString())) {
                    try {
                        ResponseEntity<String, String> responseEntity = new ResponseEntity<>(result.toString());
                        if (responseEntity.isSuccess()) {
                            ToastTip.tip(getString(R.string.appeal_send_success));
                            finish();
                        } else {
                            ToastTip.tip(responseEntity.message);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                Toast.makeText(ReportSuggestionActivity.this, "请求失败!", Toast.LENGTH_SHORT).show();
            }
        }).request();


    }
}